<template>
  <div class="">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-if="editFlag"
                   v-hasPermi="['develop:FactoryMaterialMain:add']">新增主材检验
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
              :max-height="tableMaxHeight">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column label="检验编号" align="center" prop="inspectNumber"/>
      <el-table-column label="原材料制造商" align="center" prop="producer"/>
<!--      <el-table-column label="牌号" align="center" prop="code"/>-->
<!--      <el-table-column label="炉批号" align="center" prop="heatBatchNumber"/>-->
<!--      <el-table-column label="来料检验批次号" align="center" prop="batchNumber"/>-->
      <el-table-column label="化学成分分析-C" align="center" prop="cValue"/>
      <el-table-column label="化学成分分析-P" align="center" prop="pValue"/>
      <el-table-column label="化学成分分析-S" align="center" prop="sValue"/>
      <el-table-column label="化学成分分析-Al" align="center" prop="alValue"/>
      <el-table-column label="化学成分分析-Si" align="center" prop="siValue"/>
      <el-table-column label="化学成分分析-Fe" align="center" prop="feValue"/>
      <el-table-column label="化学成分分析-Cu" align="center" prop="cuValue"/>
      <el-table-column label="化学成分分析-Mn" align="center" prop="mnValue"/>
      <el-table-column label="化学成分分析-Mg" align="center" prop="mgValue"/>
      <el-table-column label="化学成分分析-Zn" align="center" prop="znValue"/>
      <el-table-column label="化学成分分析-Ni" align="center" prop="niValue"/>
      <el-table-column label="化学成分分析-Ti" align="center" prop="tiValue"/>
      <el-table-column label="化学成分分析-Cr" align="center" prop="crValue"/>
      <el-table-column label="化学成分分析-Mo" align="center" prop="moValue"/>
      <el-table-column label="检测结果" align="center" prop="result" show-overflow-tooltip />
<!--      <el-table-column label="出厂检测日期" align="center" prop="leaveInspectDate" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.leaveInspectDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="入厂检测日期" align="center" prop="arriveInspectDate" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.arriveInspectDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column prop="fileList" label="生产厂检测报告" align="center">
        <template slot-scope="scope">
          <file-upload-show :value="scope.row.productReport"></file-upload-show>
        </template>
      </el-table-column>
      <el-table-column prop="fileList" label="来料检测报告" align="center">
        <template slot-scope="scope">
          <file-upload-show :value="scope.row.materialReport"></file-upload-show>
        </template>
      </el-table-column>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-document"
                     @click="handleDetail(scope.row)">详情
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" v-if="editFlag"
                     @click="handleUpdate(scope.row)" v-hasPermi="['develop:FactoryMaterialMain:edit']">修改
          </el-button>

          <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDelete(scope.row)" v-if="editFlag"
                         style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                       v-hasPermi="['develop:FactoryMaterialMain:remove']">删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <factoryMaterialMain-form ref="form" @ok="getList"></factoryMaterialMain-form>
    <factory-material-main-detail ref="detail" @ok="getList"></factory-material-main-detail>
  </div>
</template>

<script>
import {deleteAction, getAction} from '@/api/manage'
import FactoryMaterialMainForm from '@/views/develop/factory/factoryMaterialMain/module/FactoryMaterialMainForm'
import FactoryMaterialMainDetail
  from "@/views/develop/factory/factoryMaterialMain/module/FactoryMaterialMainDetail.vue";
import FileUploadShow from "@/components/FileUpload/FileUploadShow.vue";

export default {
  name: "FactoryMaterialMainList",
  components: {
    FileUploadShow,
    FactoryMaterialMainDetail,
    FactoryMaterialMainForm,
  },
  props: {
    workOrderId: {
      type: String,
      default: null
    },
    goodsId: {
      type: String,
      default: null
    },
    goodsSaleId: {
      type: String,
      default: null
    },
    daohuoDetailId: {
      type: String,
      default: null
    },
    materialId: {
      type: String,
      default: null
    },
    rukuId: {
      type: String,
      default: null
    },
    produceRecordDetailId: {
      type: String,
      default: null
    },
    sampleId: {
      type: String,
      default: null
    },
    editFlag:{
      type:Boolean,
      default:false
    },
  },
  watch: {
    goodsId: {
      handler(newVal, oldVal) {
        this.queryParams.goodsId = newVal;
      },
      immediate: true
    },
    goodsSaleId: {
      handler(newVal, oldVal) {
      },
      immediate: true
    },
    workOrderId: {
      handler(newVal, oldVal) {
        if(newVal){
          this.queryParams.workOrderId = newVal;
          this.getList();
        }

      },
      immediate: true
    },
    daohuoDetailId: {
      handler(newVal, oldVal) {
        this.queryParams.daohuoDetailId = newVal;
        if(newVal){
          this.getList();
        }else{
          this.dataList = [];
          this.loading = false;
        }
      },
      immediate: true
    },
    materialId: {
      handler(newVal, oldVal) {
        this.queryParams.materialId = newVal;
        if(newVal){
          this.getList();
        }
      },
      immediate: true
    },
    rukuId: {
      handler(newVal, oldVal) {
        this.queryParams.rukuId = newVal;
        if(newVal){
          this.getList();
        }
      },
      immediate: true
    },

    produceRecordDetailId: {
      handler(newVal, oldVal) {
        this.queryParams.produceRecordDetailId = newVal;
        if(newVal){
          this.getList();
        }
      },
      immediate: true
    },

    sampleId: {
      handler(newVal, oldVal) {
        this.queryParams.sampleId = newVal;
        if(newVal){
          this.getList();
        }
      },
      immediate: true
    },

  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 原材料（主材）表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        orderId: null,
        goodsId: null,
        saleId: null,
        name: null,
        type: null,
        producer: null,
        code: null,
        heatBatchNumber: null,
        batchNumber: null,
        cValue: null,
        pValue: null,
        sValue: null,
        alValue: null,
        siValue: null,
        feValue: null,
        cuValue: null,
        mnValue: null,
        mgValue: null,
        znValue: null,
        niValue: null,
        tiValue: null,
        crValue: null,
        moValue: null,
        leaveInspectDate: null,
        arriveInspectDate: null,
        qualityCertificate: null,
        tensileStrength: null,
        succumbStrength: null,
        extend: null,
        hardness: null,
        surfaceQuality: null,
        size: null,
        specs: null,
        pullStrength: null,
        extendRate: null,
        singleStrength: null,
        orderByColumn: 'code',
        isAsc: 'desc'
      },
      url: {
        list: '/factoryMaterialMain/factoryMaterialMain/list',
        remove: '/factoryMaterialMain/factoryMaterialMain/',
        export: '/factoryMaterialMain/factoryMaterialMain/export',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    /** 查询原材料（主材）列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      let workOrderId = this.workOrderId;
      let goodsId = this.goodsId;
      let daohuoDetailId = this.daohuoDetailId;
      let rukuId = this.rukuId;
      let goodsSaleId = this.goodsSaleId;
      let materialId = this.materialId;
      let sampleId = this.sampleId;
      let produceRecordDetailId = this.produceRecordDetailId;
      if (workOrderId) {
        this.$refs.form.handleAddByWorkOrderId(workOrderId,goodsId,goodsSaleId,materialId);
        return
      }
      if (daohuoDetailId) {
        this.$refs.form.handleAddByDaohuoDetailId(daohuoDetailId,goodsId,goodsSaleId,materialId);
        return
      }
      if (produceRecordDetailId) {
        this.$refs.form.handleAddByProduceDetailId(produceRecordDetailId,goodsId,goodsSaleId,materialId);
        return
      }
      if (rukuId) {
        this.$refs.form.handleAddByRukuId(rukuId,goodsId,goodsSaleId,materialId);
      }
      if (sampleId) {
        this.$refs.form.handleAddBySampleId(sampleId,goodsId,goodsSaleId,materialId);
      }
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },

    /** 详情按钮操作 */
    handleDetail(row) {
      this.$refs.detail.showDetail(row);
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(this.url.export, {
        ...this.queryParams
      }, `factoryMaterialMain_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
